﻿package string;

public class 去除字符串中连续K个0的字串 {

	public static String removeK0s(String str, int k) {
		if (str == null || str.length() == 0 || k == 0 || k > str.length())
			return str;

		char[] chrs = str.toCharArray();
		int count = 0, start = -1;
		for (int i = 0; i < chrs.length; i++) {
			if (chrs[i] == '0') {
				start = start == -1 ? i : start;
				count++;
			}
			// 如果不是0
			else {
				if (count == k) {
					while (count-- != 0) {
						// 开始清楚
						chrs[start++] = 0;
					}
				}
				count = 0;
				start = -1;
			}
		}
		//由于是在不是‘0’的位置开始删除的，因此，需要最后再判断一次
		if(count ==k){
			while(count--!=0){
				chrs[start++] =0;
			}
		}
		return String.valueOf(chrs);
	}

	public static void main(String[] args) {
			System.out.println(removeK0s("A0000B000",3));

	}

}
